﻿describe("Strategies/SimpleColoring", function () {

    var simpleColoring;

    beforeEach(function () {
        simpleColoring = new Strategies.SimpleColoring();
    });

    describe("should find when:", function () {

        it("same colors on unit", function () {
            var grid = "-75-469-834629817589--57-4676952481352387146941896375263-48259-98--15--4-54-39-8-";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = simpleColoring.perform(gridModel);

            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(2);
            expect(performResult.conflicts.length).toEqual(1);

            var conflictingCells = performResult.conflicts[0].conflictingCells;
            var expectedConflictingCells = [gridModel.cells[0][0], gridModel.cells[7][7], gridModel.cells[2][6], gridModel.cells[8][6], gridModel.cells[7][2]];
            expect(conflictingCells).toEqual(jasmine.arrayContaining(expectedConflictingCells));

            expect(performResult.seeTwoColors.length).toEqual(0);

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[0][0].candidates.remove(2);
            expectedGridModel.cells[2][6].candidates.remove(2);
            expectedGridModel.cells[7][2].candidates.remove(2);
            expectedGridModel.cells[7][7].candidates.remove(2);
            expectedGridModel.cells[8][6].candidates.remove(2);

            expect(gridModel).toEqual(expectedGridModel);
        });

        it("a cell can see two different colors", function () {
            var grid = "--7-836---397-68--82641975364-19-387-8-367----73-48-6-39-87--267649--1382-863-97-";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = simpleColoring.perform(gridModel);
            
            //expect
            expect(performResult).toBeTruthy();
            expect(performResult.candidate).toEqual(2);
            expect(performResult.conflicts.length).toEqual(0);

            var cellsSeeTwo = performResult.seeTwoColors.map(function (obj) { return obj.cell; });
            expect(cellsSeeTwo).toEqual(jasmine.arrayContaining([performResult.seeTwoColors[0].cell, performResult.seeTwoColors[1].cell]));
            
            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[4][8].candidates.remove(2);
            expectedGridModel.cells[5][8].candidates.remove(2);

            expect(gridModel).toEqual(expectedGridModel);
        });

    });
});

